package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.search.AlbumItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.concurrent.*;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private ExecutorService executorService;

    @Override
    public AlbumItemVo loadData(Long albumId) {
        AlbumItemVo albumItemVo = new AlbumItemVo();

        //1.根据专辑id查询专辑
        CompletableFuture<AlbumInfo> albumFuture = CompletableFuture.supplyAsync(() -> {
            Result<AlbumInfo> albumInfoResult = this.albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(albumInfoResult, "专辑详情页,获取专辑信息失败! " + albumId);
            AlbumInfo albumInfo = albumInfoResult.getData();
            Assert.notNull(albumInfo, "专辑详情页,对应的专辑不存在! " + albumId);
            albumItemVo.setAlbumInfo(albumInfo);
            return albumInfo;
        }, executorService);


        //2.查询三级分类
        CompletableFuture<Void> categoryFuture = albumFuture.thenAcceptAsync(albumInfo -> {
            Result<BaseCategoryView> baseCategoryViewResult = this.categoryFeignClient.queryCategoryView(albumInfo.getCategory3Id());
            Assert.notNull(baseCategoryViewResult, "专辑详情页,获取三级分类信息失败! " + albumInfo.getCategory3Id());
            BaseCategoryView baseCategoryView = baseCategoryViewResult.getData();
            albumItemVo.setBaseCategoryView(baseCategoryView);
        }, executorService);


        //3.查询统计信息
        CompletableFuture<Void> statFuture = CompletableFuture.runAsync(() -> {
            Result<AlbumStatVo> albumStatVoResult = this.albumInfoFeignClient.queryAlbumStats(albumId);
            Assert.notNull(albumStatVoResult, "专辑详情页,获取统计信息失败! " + albumId);
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            albumItemVo.setAlbumStatVo(albumStatVo);
        }, executorService);

        //4.查询主播信息
        CompletableFuture<Void> userFuture = albumFuture.thenAcceptAsync(albumInfo -> {
            Result<UserInfo> userInfoResult = this.userInfoFeignClient.queryUserById(albumInfo.getUserId());
            Assert.notNull(userInfoResult, "专辑详情页,获取主播信息失败! " + albumInfo.getUserId());
            UserInfo userInfo = userInfoResult.getData();
            albumItemVo.setAnnouncer(userInfo);
        },executorService);

        CompletableFuture.allOf(categoryFuture, statFuture, userFuture).join();
        return albumItemVo;
    }
    public static void main(String[] args) {

//        new MyThread().start();
//
//        new Thread(() -> {
//            new FileInputStream("xxxxx");
//            return "hello";
//        }).start();

//        FutureTask<String> futureTask = new FutureTask<>(() -> {
//            System.out.println("1这是Callable实现了多线程程序！" + Thread.currentThread().getName());
//            int i = 1/0;
//            return "hello Callalbe";
//        });
//        new Thread(futureTask).start();
//
//        try {
//            // 阻塞方式获取子线程的结果集及异常信息
//            System.out.println("3主线程中获取子线程的结果集：" + futureTask.get());
//        } catch (Exception e) {
//            System.out.println("4主线程中获取子线程的异常信息：" + e.getMessage());
//        }

        ExecutorService executorService = new ThreadPoolExecutor(10, 15, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(20));

        System.out.println("2这是main方法：" + Thread.currentThread().getName());
    }
}

class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println("子线程任务：" + Thread.currentThread().getName());
    }
}

